package cn.edu.zafu.bigdata.lekang.shiyan8;

import java.io.*;
import java.net.*;

public class TimeClient {
    public static void main(String[] args) {
        try {
            // 创建一个DatagramSocket
            DatagramSocket clientSocket = new DatagramSocket();
            // 创建一个byte数组用于发送请求
            byte[] sendData = "What time is it?".getBytes();
            // 创建一个byte数组用于接收响应
            byte[] receiveData = new byte[1024];

            // 获取服务器的IP地址
            InetAddress serverAddress = InetAddress.getByName("192.168.16.75");

            // 发送请求到服务器
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 9876);
            clientSocket.send(sendPacket);

            // 接收服务器的响应
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            // 读取服务器发送的数据
            String response = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("Server says: " + response);

            // 关闭客户端socket
            clientSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Server not found: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("I/O Error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
